package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 老钱庄
 * @author Persh
 *
 */
public class _538538NewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.538538.com/Stock_.*.html\\?stock=[a-z]*&info=.*",
		"http://www.538538.com/Stock_.*.html\\?info=.*&stock=[a-z]*",
		"http://www.538538.com/Stock_.*.html\\?CompanyCode=[0-9]*&gather=[0-9]*&id=[0-9]*",
		"http://.*.laoqianzhuang.com/.*/[0-9]*/[0-9]*.shtml"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("table#allbulletin thead").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select(".content_title").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("h5").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.atc-content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#contentText"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#box"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".content_main"));
		}
		author=JSoupUtils.matchAuthor(doc, "来源：");
		date=DateUtils.matchDate(doc.select("span#pubtime_baidu").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select(".content_time").text());
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.info").text());
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		_538538NewsAnalyse analyse=new _538538NewsAnalyse();
		String url="http://stock.laoqianzhuang.com/redianticai/20150629/802101.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
